﻿using Che.Domain.Entity.EventsBut;
using Che.Domain.repository.Football;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Distributed;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace Che.EFCore.Repository.Football
{
    public class EFfootball : IFootball
    {
        private readonly MyDbContext dbContext;
        private readonly ISqlSugarClient sugarClient;
        private readonly IDistributedCache distributedCache;
        public EFfootball(MyDbContext dbContext, ISqlSugarClient sugarClient, IDistributedCache distributedCache)
        {
            this.dbContext = dbContext;
            this.sugarClient = sugarClient;
            this.distributedCache = distributedCache;
        }

        public Task<bool> addFottball(Domain.Entity.EventsBut.Football football)
        {
            sugarClient.Insertable(football).IgnoreColumns(ignoreNullColumn: true).ExecuteCommand();
            return Task.FromResult(true);
        }

        public async Task<List<Footballquest>> foootballquest()
        {
            var result = await (from football in dbContext.Fottball
                                join stadium in dbContext.Stadium on football.PlayingvenuesId equals stadium.StadiumId
                                join eventsType in dbContext.Eventstype on football.LeagueMatchesid equals eventsType.EventsTypeId
                                select new Footballquest
                                {
                                    FootballId = football.FootballId,
                                    FootballName = football.FootballName,
                                    PlayingvenuesId = football.PlayingvenuesId,
                                    Founded = football.Founded,
                                    LeagueMatchesid = football.LeagueMatchesid,
                                    ContinentalId = football.ContinentalId,
                                    Numberoftrophies = football.Numberoftrophies,
                                    Src = football.Src,
                                    EventsTypeName = eventsType.EventsTypeName,
                                    StadiumName = stadium.StadiumName
                                }).ToListAsync();

            return result;
        }
        /// <summary>
        /// 查询足球球队信息
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<Domain.Entity.EventsBut.Footballquest>> QustFottball()
        {
           
            const string Keys = "Footballkey";
            var CacheDate = await distributedCache.GetStringAsync(Keys);
            if (!string.IsNullOrEmpty(CacheDate))
            {
                var Data = JsonSerializer.Deserialize<List<Domain.Entity.EventsBut.Footballquest>>(CacheDate);
                return Data;
            }
            //sqlsugar查询球队表以及相关的·
            var Dates = sugarClient.Queryable<Domain.Entity.EventsBut.Football>()
            .LeftJoin<Stadium>((o, te) => o.PlayingvenuesId == te.StadiumId)
            .LeftJoin<EventType>((o, te, ev) => o.LeagueMatchesid == ev.EventsTypeId)
            .Select((o, te, ev) => new Footballquest
            {
                FootballId = o.FootballId,
                FootballName = o.FootballName,
                PlayingvenuesId = o.PlayingvenuesId,
                Founded = o.Founded,
                LeagueMatchesid = o.LeagueMatchesid,
                ContinentalId = o.ContinentalId,
                Numberoftrophies = o.Numberoftrophies,
                Src = o.Src,
                StadiumName = te.StadiumName,
                EventsTypeName = ev.EventsTypeName
            }).ToList();


            var stringfootball = JsonSerializer.Serialize(Dates);
            await distributedCache.SetStringAsync(Keys, stringfootball, new DistributedCacheEntryOptions
            {
                AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(10)

            });
            return Dates;
        }
    }
}
